﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace VkSample98
{
    public class InputSystem : ComponentSystem
    {
        private Camera Camera;
        private IGraphicsManager GraphicsManager;
        private bool Fource = true;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contextManager"></param>
        public InputSystem(IGraphicsManager manager)
        {
            GraphicsManager = manager;
        }

        public override void Initialization()
        {
            GraphicsManager.TryGetCamera("main", out Camera);
        }

        public override void Update(TimeSpan timeSpan)
        {
            if (Fource)
            {
                var delta = Input.GetMousePositionDelta();
                if (delta.X != 0 || delta.Y != 0)
                {
                    Camera.RotationFromMouse(delta.X, delta.Y, (float)(0.02 * Math.PI / 180.0));
                }
            }
            if (Input.GetPressKeyState(PressKey.W).Action == InputAction.Press)
            {
                Camera.ParallelTranslation(0, -1.5f);
            }
            if (Input.GetPressKeyState(PressKey.S).Action == InputAction.Press)
            {
                Camera.ParallelTranslation(0, 1.5f);
            }
            if (Input.GetPressKeyState(PressKey.A).Action == InputAction.Press)
            {
                Camera.ParallelTranslation(-1.5f, 0);
            }
            if (Input.GetPressKeyState(PressKey.D).Action == InputAction.Press)
            {
                Camera.ParallelTranslation(1.5f, 0);
            }
            if (Input.GetPressKeyState(PressKey.KeyKpLeftAlt).Action == InputAction.Click)
            {
                Fource = !Fource;
                Input.CursorDisabled(!Fource);
            }
            ContextManager.Foreach<Group>((gourp) =>
            {
                Context context = ContextManager.GetContext(gourp.Entity);
                context.PushToRenderGroup();
            });
        }
    }

    public struct Group
    {
        public Entity Entity;
        public TransformComponent Transform;
    }
}
